/*
*
*  name: ambad
*  author: Alinejun
*  date: 2018/08/10 10:30
*
*/


// 判断手机访问
function browserRedirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
       return true;
    } else {
       return false;
    }
}


$(function(){

    baseMotheds.init();

});


// baseMotheds
var baseMotheds = function(){

    // scrollHead

    var scrollHead = function(){

        if (browserRedirect()) {return;}

        var headGroup = $('.header').height();

        var headFullTabs = $('#category').height();

        var sroll = headGroup-headFullTabs;

        $(window).bind("scroll resize", function() {

        	if ($(window).scrollTop() > sroll) {

        		$('#category').addClass('index');

        		$('.container').eq(0).css('padding-top','50px');

        	}else{

        		$('#category').removeClass('index');

        		$('.container').eq(0).css('padding-top','0');

        	}

        });

    };

    // ripples
    var ripples = function(){

        if (browserRedirect()) {return;}

        try {
            $('body').ripples({
                resolution: 512,
                dropRadius: 15,
                perturbance: 0.05,
            });
        }catch (e) {
            $('.footer .error').show().text(e);
        }

        // $('.disable').show().on('click', function() {
        //     $('body, main').ripples('destroy');
        //     $(this).hide();
        // });
    }

    // lazyLoad
    var lazyLoad = function(){
        $("img.lazy").lazyload({threshold: 100,effect: "fadeIn"});
    };

    var imgError = function(){
        document.addEventListener("error", function (e) {
            var elem = e.target;
            if (elem.tagName.toLowerCase() == "img") {
                elem.src = "http://www.ambad.cn/public/static/index/images/headpic/temp7.jpg";
            }
        }, true);

    };

    var imgNull = function(){

        $('img.lazy').each(function(){
            var _newImg = 'http://www.ambad.cn/public/static/index/images/headpic/temp7.jpg';
            var _thisSrc = $(this).attr('src');
            if(_thisSrc == '' || _thisSrc == undefined){
                $(this).attr('src',_newImg);
            }

        });

        document.addEventListener("error", function (e) {
            var elem = e.target;
            if (elem.tagName.toLowerCase() == "img") {
                elem.src = "http://www.ambad.cn/public/static/index/images/headpic/temp7.jpg";
            }
        }, true);

    };

    return{

        init: function(){

            scrollHead();

            // ripples();

            imgError();

            imgNull();

        }

    }

}();



